package com.iflyun.LogsCustmerToHDFS.utils;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Base64;


/**
 * 
 * @ClassName:  ThreeDESUtils   
 * @Description:3des加密工具类
 * @author: wenquxing  
 * @date:   2017年8月25日 上午9:55:04   
 *
 */
public class ThreeDESUtils {
	
	private static String key="7rboiRgZeueTqAUJ5sIhaaXW8okqmF0z";
	
	private static byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
	
	public static void main(String[] args) throws Exception { 

          
        String data="test";  
          
        System.out.println("ECB加密解密");  
        String str3 = des3EncodeECB(data );  
        String str4 = des3DecodeECB(str3);  
        System.out.println(str3);  
        System.out.println(str4);  
        System.out.println("-----------------------------");  
        System.out.println("CBC加密解密");  
        String str5 = des3EncodeCBC(data);  
        String str6 = des3DecodeCBC(str5);  
        System.out.println(str5);  
        System.out.println(str6);  
    }  
    /** 
     * ECB加密,不要IV 
     * @param key 密钥 
     * @param data 明文 
     * @return Base64编码的密文 
     * @throws Exception 
     */  
    public static String des3EncodeECB(String str)  
            throws Exception {
        Key deskey = null;  
        byte[] data=str.getBytes("UTF-8");
        DESedeKeySpec spec = new DESedeKeySpec(Base64.decodeBase64(key));  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");  
        cipher.init(Cipher.ENCRYPT_MODE, deskey);  
        byte[] bOut = cipher.doFinal(data);         
        return Base64.encodeBase64String(bOut);  
    }  
    /** 
     * ECB解密,不要IV 
     * @param key 密钥 
     * @param data Base64编码的密文 
     * @return 明文 
     * @throws Exception 
     */  
    public static String des3DecodeECB(String str)  
            throws Exception {  
        Key deskey = null;  
        byte[] data=Base64.decodeBase64(str);
        DESedeKeySpec spec = new DESedeKeySpec(Base64.decodeBase64(key));  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");  
        cipher.init(Cipher.DECRYPT_MODE, deskey);  
        byte[] bOut = cipher.doFinal(data); 
        String result=new String(bOut, "UTF-8");
        return result; 
    }  
    /** 
     * CBC加密 
     * @param key 密钥 
     * @param keyiv IV 
     * @param data 明文 
     * @return Base64编码的密文 
     * @throws Exception 
     */  
    public static String des3EncodeCBC(String str)  
            throws Exception {  
        Key deskey = null;  
        byte[] data=str.getBytes("UTF-8");
        DESedeKeySpec spec = new DESedeKeySpec(Base64.decodeBase64(key));  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(keyiv);  
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
        byte[] bOut = cipher.doFinal(data);  
        return Base64.encodeBase64String(bOut); 
    }  
    /** 
     * CBC解密 
     * @param key 密钥 
     * @param keyiv IV 
     * @param data Base64编码的密文 
     * @return 明文 
     * @throws Exception 
     */  
    public static String des3DecodeCBC( String str)  
            throws Exception {  
        Key deskey = null;  
        byte[] data=Base64.decodeBase64(str);
        DESedeKeySpec spec = new DESedeKeySpec(Base64.decodeBase64(key));  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(keyiv);  
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
        byte[] bOut = cipher.doFinal(data);  
        String result=new String(bOut, "UTF-8");
        return result; 
    }  
}
